# fastlane {name} [version:1.0.0] [build:1.0.0.1] [update:Changelog] [skip:true]
# https://docs.fastlane.tools
# update_fastlane
# 授权方式一：使用api_key的方式授权
# 1. http://docs.fastlane.tools/app-store-connect-api/
# 授权方式二：默认使用两步验证，有效期一个月，可运行如下命令手工授权
# 1. fastlane spaceauth
# 授权方式三：配置.env专用密码+skip_waiting_for_build_processing+apple_id
# 1. FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD="{PASSWORD}"

# config
PGYER_APIKEY="{PGYER_APIKEY}"
PGYER_USERKEY="{PGYER_USERKEY}"
PGYER_PASSWD="{PGYER_PASSWD}"
APPSTORE_ID="{APPSTORE_ID}"

# .env
# PGYER_APIKEY="{PGYER_APIKEY}"
# PGYER_USERKEY="{PGYER_USERKEY}"
# PGYER_PASSWD="{PGYER_PASSWD}"
# APPSTORE_ID="{APPSTORE_ID}"
# Fastfile
# ENV["PGYER_APIKEY"]

default_platform(:ios)

platform :ios do
  before_all do |lane, options|
    if options[:version]
      VERSION_NUMBER = options[:version]
      increment_version_number(version_number: VERSION_NUMBER)
    elsif options[:skip]
      VERSION_NUMBER = ""
    else
      VERSION_NUMBER = get_version_number
    end

    if options[:build]
      BUILD_NUMBER = options[:build]
      increment_build_number(build_number: BUILD_NUMBER, skip_info_plist: true)
    elsif options[:skip]
      BUILD_NUMBER = ""
    else
      BUILD_NUMBER = get_build_number
    end

    puts "Version: #{VERSION_NUMBER} Build: #{BUILD_NUMBER}"

    if options[:update]
      CHANGELOG_TEXT = options[:update]
    elsif options[:skip]
      CHANGELOG_TEXT = ""
    else
      CHANGELOG_TEXT = prompt(text: "Changelog:", multi_line_end_keyword: "END")
    end

    CURRENT_DATE = Time.new.strftime("%Y-%m-%d")
    if "#{lane}".include? "testing"
      CHANGELOG_TEXT = "[#{BUILD_NUMBER}+testing] - #{CURRENT_DATE}\n\n#{CHANGELOG_TEXT}"
    else
      CHANGELOG_TEXT = "[#{BUILD_NUMBER}+production] - #{CURRENT_DATE}\n\n#{CHANGELOG_TEXT}"
    end

    puts "Changelog: #{CHANGELOG_TEXT}"
    cocoapods(repo_update: true, clean_install: true, use_bundle_exec: false)
  end

  desc "Push a new testing build to Pgyer"
  lane :pgyer_testing do |options|
    build_app(
      workspace: "Example.xcworkspace",
      scheme: "Example",
      configuration: "Testing",
      export_method: "ad-hoc",
      export_xcargs: "-allowProvisioningUpdates",
      output_directory: "fastlane/build/",
      output_name: "Example_pgyer_testing.ipa",
      include_symbols: false,
      include_bitcode: false,
      silent: true,
      clean: true
    )
    pgyer(
      api_key: PGYER_APIKEY,
      user_key: PGYER_USERKEY,
      password: PGYER_PASSWD,
      install_type: "2",
      update_description: CHANGELOG_TEXT
    )
  end

  desc "Push a new production build to Pgyer"
  lane :pgyer_production do |options|
    build_app(
      workspace: "Example.xcworkspace",
      scheme: "Example",
      configuration: "Release",
      export_method: "ad-hoc",
      export_xcargs: "-allowProvisioningUpdates",
      output_directory: "fastlane/build/",
      output_name: "Example_pgyer_production.ipa",
      include_symbols: false,
      include_bitcode: false,
      silent: true,
      clean: true
    )
    pgyer(
      api_key: PGYER_APIKEY,
      user_key: PGYER_USERKEY,
      password: PGYER_PASSWD,
      install_type: "2",
      update_description: CHANGELOG_TEXT
    )
  end

  desc "Push a new testing build to TestFlight"
  lane :testflight_testing do |options|
    build_app(
      workspace: "Example.xcworkspace",
      scheme: "Example",
      configuration: "Staging",
      export_method: "app-store",
      export_xcargs: "-allowProvisioningUpdates",
      output_directory: "fastlane/build/",
      output_name: "Example_testflight_testing.ipa",
      silent: true,
      clean: true
    )
    if options[:update]
      upload_to_testflight(
        skip_waiting_for_build_processing: false,
        changelog: CHANGELOG_TEXT,
        skip_submission: true
      )
    else
      upload_to_testflight(
        skip_waiting_for_build_processing: true,
        apple_id: APPSTORE_ID,
        skip_submission: true
      )
    end
  end

  desc "Push a new production build to TestFlight"
  lane :testflight_production do |options|
    build_app(
      workspace: "Example.xcworkspace",
      scheme: "Example",
      configuration: "Release",
      export_method: "app-store",
      export_xcargs: "-allowProvisioningUpdates",
      output_directory: "fastlane/build/",
      output_name: "Example_testflight_production.ipa",
      silent: true,
      clean: true
    )
    if options[:update]
      upload_to_testflight(
        skip_waiting_for_build_processing: false,
        changelog: CHANGELOG_TEXT,
        skip_submission: true
      )
    else
      upload_to_testflight(
        skip_waiting_for_build_processing: true,
        apple_id: APPSTORE_ID,
        skip_submission: true
      )
    end
  end

  desc "Push a new production build to the App Store"
  lane :appstore_production do |options|
    build_app(
      workspace: "Example.xcworkspace",
      scheme: "Example",
      configuration: "Release",
      export_method: "app-store",
      export_xcargs: "-allowProvisioningUpdates",
      output_directory: "fastlane/build/",
      output_name: "Example_appstore_production.ipa",
      silent: true,
      clean: true
    )
    upload_to_app_store(
      skip_metadata: true,
      skip_screenshots: true,
      skip_app_version_update: true
    )
  end
end
